package com.kelvin.paopao.module.main.find.list;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.paopao.PaoPaoApplication;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.itemdecoration.SpacesItemDecoration;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.NearByPersonModel;
import com.kelvin.paopao.injector.components.DaggerMainFindListComponent;
import com.kelvin.paopao.injector.modules.MainFindListModule;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.login.LoginAndRegisterActivity;
import com.kelvin.paopao.module.person.detail.PersonDetailActivity;
import com.kelvin.paopao.utils.Constant;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/13.
 */

public class MainFindListFragment extends BaseFragment<MainFindListPresenter> implements IMainFindListView,
        BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    public static final String TYPE_NEW = "new";
    public static final String TYPE_NEAR = "near";
    public static final String TYPE_FREE = "free";
    public static final String TYPE_TUIJIAN = "tuijian";
    public static final String TYPE_RENZHENG = "renzheng";

    @BindView(R.id.ll_login_register)
    LinearLayout llLoginRegister;

    private String type;

    @Inject
    BaseQuickAdapter adapter;
    private LinearLayoutManager manager;


    public static MainFindListFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString("type", type);

        MainFindListFragment fragment = new MainFindListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_find_list;
    }

    @Override
    protected void initInjector() {
        DaggerMainFindListComponent.builder().applicationComponent(getAppComponent())
                .mainFindListModule(new MainFindListModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        type = getArguments().getString("type");

        manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
//        recyclerView.addItemDecoration(new SpacesItemDecoration(15));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PersonDetailActivity.launch(getContext(), ((List<NearByPersonModel>) adapter.
                        getData()).get(position).getUser_no());
            }
        });
        adapter.setOnLoadMoreListener(this, recyclerView);
        recyclerView.setAdapter(adapter);


        //如果没有登录，显示登录栏
        if (!Constant.IS_LOGIN) {
            llLoginRegister.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {
//        mPresenter.getData(isRefresh, type);
    }

    @Override
    public void loadData(CommonListResponse<NearByPersonModel> response) {
        adapter.setNewData(response.getResult());
    }

    @Override
    public void loadMoreData(CommonListResponse<NearByPersonModel> response) {
        adapter.addData(response.getResult());
        adapter.loadMoreComplete();
    }

    @Override
    public void showNoData() {
        View nodataView = LayoutInflater.from(getContext()).inflate(R.layout.view_no_data,
                (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(nodataView);
        adapter.setNewData(null);
    }


    @Override
    public void onLoadMoreRequested() {
        if (adapter.getData().size() < 30) {
            //不再加载更多
            adapter.loadMoreEnd(true);
        } else {
            mPresenter.getMoreData();
        }
    }

    @OnClick({R.id.tv_login, R.id.tv_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                LoginAndRegisterActivity.launch(getContext());
                break;
            case R.id.tv_register:
                LoginAndRegisterActivity.launch(getContext());
                break;
        }
    }

    public void getDataWithFilter(String user_sex, String user_tizhong, String user_shengao, String user_label) {
        mPresenter.getDataWithFilter(user_sex, user_tizhong, user_shengao, user_label);
    }
}
